import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'color.dart';

void refreshCompelet(RefreshController refreshController) {
  if (refreshController.isRefresh) {
    refreshController.refreshCompleted(resetFooterState: true);
  }
  if (refreshController.isLoading) {
    refreshController.loadComplete();
  }
}

///刷新控件封装
Widget elRefresh(
    {refreshController,
    child,
    onRefresh,
    onLoading,
    noDataText, //自定义加载完数据显示的文本
    pageNum, //当前加载页数
    enablePullUp = true,
    enablePullDown = true,
    height}) {
  return SmartRefresher(
    controller: refreshController,
    enablePullUp: enablePullUp,
    enablePullDown: enablePullDown,
    header: Platform.isAndroid
        ? MaterialClassicHeader(
            color: ColorManager.colorD93C3A,
            height: height ?? 80,
          )
        : CustomHeader(
            height: 35.w,
            builder: (BuildContext context, RefreshStatus? mode) {
              Widget header;
              // LogUtils.d(mode);
              if (mode == RefreshStatus.idle) {
                header = const Text("松手,加载!");
              } else if (mode == RefreshStatus.refreshing) {
                header = const CupertinoActivityIndicator();
              } else if (mode == RefreshStatus.failed) {
                header = const Text("加载失败！点击重试！");
              } else if (mode == RefreshStatus.canRefresh) {
                header = const Text("松手,加载!");
              } else {
                header = const Text("加载完成");
              }
              return Container(
                alignment: Alignment.center,
                child: header,
              );
            },
          ),
    footer: CustomFooter(
      height: 50.w,
      builder: (BuildContext context, LoadStatus? mode) {
        Widget body;
        if (mode == LoadStatus.idle) {
          body = const Text("上拉加载");
        } else if (mode == LoadStatus.loading) {
          body = const CupertinoActivityIndicator();
        } else if (mode == LoadStatus.failed) {
          body = const Text("加载失败！点击重试！");
        } else if (mode == LoadStatus.canLoading) {
          body = const Text("松手,加载更多!");
        } else {
          body = Text(pageNum != 1 ? noDataText ?? '没有更多数据了' : '');
        }
        return Container(
          height: 55.0,
          alignment: Alignment.center,
          child: body,
        );
      },
    ),
    child: child ?? Container(),
    onRefresh: onRefresh,
    onLoading: onLoading,
  );
}
